package com.haohan.jingyuan.customer.helper;

import com.haohan.jingyuan.customer.dto.resp.FileShareInfoResp;
import com.haohan.jingyuan.customer.entity.CustomerFile;
import com.haohan.jingyuan.customer.entity.CustomerFileShare;
import com.haohan.jingyuan.customer.entity.CustomerGroupInfo;

import java.util.Map;

/**
 * 文件分享响应对象转换工具类
 * 
 * @ClassName FileShareRespHelper
 * @Description 文件分享响应对象转换工具
 * @Author hexijun
 * @Time 2025/11/04 17:00
 * @Version 1.0
 */
public class FileShareRespHelper {

    /**
     * 将文件分享实体转换为文件分享信息响应对象
     * 
     * @param customerFileShare 文件分享实体
     * @return 文件分享信息响应对象
     */
    public static FileShareInfoResp convert2FileShareInfoResp(CustomerFileShare customerFileShare) {
        return convert2FileShareInfoResp(customerFileShare, null);
    }

    /**
     * 将文件分享实体转换为文件分享信息响应对象（带分组名称）
     * 
     * @param customerFileShare 文件分享实体
     * @param groupMap 分组信息Map，key为groupId，value为CustomerGroupInfo
     * @return 文件分享信息响应对象
     */
    public static FileShareInfoResp convert2FileShareInfoResp(CustomerFileShare customerFileShare, Map<String, CustomerGroupInfo> groupMap) {
        return convert2FileShareInfoResp(customerFileShare, groupMap, null);
    }

    /**
     * 将文件分享实体转换为文件分享信息响应对象（带分组名称和文件信息）
     * 
     * @param customerFileShare 文件分享实体
     * @param groupMap 分组信息Map，key为groupId，value为CustomerGroupInfo
     * @param fileMap 文件信息Map，key为fileKey，value为CustomerFile
     * @return 文件分享信息响应对象
     */
    public static FileShareInfoResp convert2FileShareInfoResp(CustomerFileShare customerFileShare, Map<String, CustomerGroupInfo> groupMap, Map<String, CustomerFile> fileMap) {
        if (customerFileShare == null) {
            return null;
        }

        FileShareInfoResp fileShareInfoResp = new FileShareInfoResp();
        fileShareInfoResp.setId(customerFileShare.getId());
        fileShareInfoResp.setShareId(customerFileShare.getShareId());
        fileShareInfoResp.setVisitCode(customerFileShare.getVisitCode());
        fileShareInfoResp.setFileKey(customerFileShare.getFileKey());
        
        // 设置文件名称
        if (fileMap != null && customerFileShare.getFileKey() != null && !customerFileShare.getFileKey().trim().isEmpty()) {
            CustomerFile customerFile = fileMap.get(customerFileShare.getFileKey());
            if (customerFile != null) {
                fileShareInfoResp.setFileName(customerFile.getFileName());
            }
        }
        
        fileShareInfoResp.setCustomerId(customerFileShare.getCustomerId());
        fileShareInfoResp.setGroupCode(customerFileShare.getGroupCode());
        fileShareInfoResp.setQrCodeOssKey(customerFileShare.getQrCodeOssKey());
        fileShareInfoResp.setExpireAt(customerFileShare.getExpireAt());
        fileShareInfoResp.setCreator(customerFileShare.getCreator());
        fileShareInfoResp.setGmtCreated(customerFileShare.getGmtCreated());
        fileShareInfoResp.setModifier(customerFileShare.getModifier());
        fileShareInfoResp.setGmtModified(customerFileShare.getGmtModified());
        fileShareInfoResp.setIsDeleted(customerFileShare.getIsDeleted());

        // 设置分组名称
        if (groupMap != null && customerFileShare.getGroupCode() != null && !customerFileShare.getGroupCode().trim().isEmpty()) {
            CustomerGroupInfo groupInfo = groupMap.get(customerFileShare.getGroupCode());
            if (groupInfo != null) {
                fileShareInfoResp.setGroupName(groupInfo.getGroupName());
            }
        }

        return fileShareInfoResp;
    }
}

